Skip to content

Take the bit width in rotate_left/rotate_right from the rotated value - #700

Merged
Firestar99 merged 1 commit into
Rust-GPU:mainfrom
greenhat:fix-rotate-width
Nov 16, 2026
Merged

Firestar99 merged 1 commit into
Rust-GPU:mainfrom
greenhat:fix-rotate-width

Conversation

@greenhat

Copy link
Copy Markdown
Contributor

Builder::rotate (builder/mod.rs) took the bit width from the type of the shift amount instead of the type of the rotated value. The shift amount is always u32, so every rotate was done modulo 32. 0x9ABCDEF0_12345678u64.rotate_left(1) returns 0x3579BDE1_3579BDF0 instead of 0x3579BDE0_2468ACF1.

This takes the bit width from the rotated value. The new difftest lang/core/intrinsics/rotate runs 8, 16, 32 and 64-bit rotates in a shader and compares the results with the CPU. It fails without the fix.

The fix and the test were written with an AI coding agent. I reviewed and checked every line.

Details

The code emitted for a u64 before the fix:

%44 = OpBitwiseAnd %u32 %43 %u32_31
%45 = OpISub %u32 %u32_32 %44
%46 = OpShiftLeftLogical %u64 %40 %44
%47 = OpShiftRightLogical %u64 %40 %45

The constants are now 63 and 64 for u64, 15 and 16 for u16, 7 and 8 for u8. The code emitted for u32 does not change (the expected output of ui/dis/issue-1062 is the same).

u8 and u16 gave the right values on lavapipe, but they were shifted by amounts at or above their bit width, and the result of such a shift is not defined in SPIR-V.

The constants keep the type of the shift amount, since they are combined with it. OpShiftLeftLogical/OpShiftRightLogical do not require Shift to have the bit width of Base.

The test reads the amounts from a buffer (below, at and above each bit width). Without the fix 242 of the 448 output words differ, all of them in the 64-bit results. It follows trailing_zeros_64: Ash backend (it needs Int8, Int16 and Int64), skipped on macOS. I ran it on lavapipe only.

I removed the TODO: Definitely add tests to make sure this impl is right. comment on rotate, since there is a test now.

@Firestar99 Firestar99 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleaned up the claude-isms

I'll wait with pipelines until main created the new rust-cache

@Firestar99
Firestar99 added this pull request to the merge queue Nov 16, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Nov 16, 2026
`Builder::rotate` took the bit width from the type of the shift amount,
which is `u32` for every value type. So 64-bit values were rotated modulo
32, and 8 and 16-bit values were shifted by amounts at or above their bit
width. Take the bit width from the type of the rotated value instead.

Add the difftest `lang/core/intrinsics/rotate` that runs 8, 16, 32 and
64-bit rotates in a shader and compares the results with the CPU.
@Firestar99
Firestar99 merged commit 3e006f7 into Rust-GPU:main Nov 16, 2026
22 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants